﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BlueLotus7.CDKey
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        CDKeyManager manager = new CDKeyManager();
        CDKeyObject currentRenderedObject = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshApplicationNameList();
        }

        private void RefreshApplicationNameList()
        {
            this.comboBox_ApplicationNameList.ItemsSource = manager.ApplicationNameList;
            this.comboBox_ApplicationNameList.DisplayMemberPath = "Display";
            this.comboBox_ApplicationNameList.SelectedValuePath = "Key";
        }

        private void RefreshResultList()
        {
            string applicationName = comboBox_ApplicationNameList.SelectedItem == null ? string.Empty : ((ApplicationName)(comboBox_ApplicationNameList.SelectedItem)).Key;
            bool isVerifiedOnly = checkBox_VerifiedOnly.IsChecked.HasValue ? checkBox_VerifiedOnly.IsChecked.Value : false;
            bool isOfficialOnly = checkBox_OfficialOnly.IsChecked.HasValue ? checkBox_OfficialOnly.IsChecked.Value : false;

            List<CDKeyObject> result = manager.QueryCDKey(applicationName, isVerifiedOnly, isOfficialOnly);
            FillListView(result);
        }

        private void FillListView(List<CDKeyObject> objList)
        {
            if (objList != null)
            {
                listView_QueryResult.ItemsSource = objList;
            }
        }

        private void FillList(Grid grid, List<string> headers, List<CDKeyObject> cdKeyObjects)
        {
            if (grid != null && cdKeyObjects != null && headers != null)
            {
                grid.ColumnDefinitions.Clear();
                grid.RowDefinitions.Clear();

                ColumnDefinition columnKey = new ColumnDefinition();
                columnKey.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(columnKey);

                ColumnDefinition columnApplicationName = new ColumnDefinition();
                columnApplicationName.Width = new GridLength(200, GridUnitType.Pixel);
                grid.ColumnDefinitions.Add(columnApplicationName);

                //header
                for (int i = 0; i < headers.Count; i++)
                {
                    TextBlock headerCell = new TextBlock();
                    headerCell.Text = headers[i];
                    headerCell.FontSize = 14;
                    headerCell.FontWeight = FontWeights.Bold;
                    headerCell.Foreground = new SolidColorBrush(Colors.Green);
                    headerCell.VerticalAlignment = VerticalAlignment.Top;
                    Grid.SetRow(headerCell, 0);
                    Grid.SetColumn(headerCell, i);
                }

                //body
                for (int i = 0; i < cdKeyObjects.Count; i++)
                {
                    TextBlock bodyCell = new TextBlock();
                    bodyCell.Text = headers[i];
                    bodyCell.FontSize = 14;
                    bodyCell.FontWeight = FontWeights.Bold;
                    bodyCell.Foreground = new SolidColorBrush(Colors.Green);
                    bodyCell.VerticalAlignment = VerticalAlignment.Top;
                    Grid.SetRow(bodyCell, 0);
                    Grid.SetColumn(bodyCell, i);
                }
            }
        }

        private void button_AddNew_Click(object sender, RoutedEventArgs e)
        {
            FillCDKeyObject(null);
        }

        private void FillCDKeyObject(CDKeyObject obj)
        {
            bool isNew = false;
            if (obj == null)
            {
                isNew = true;
                obj = new CDKeyObject();
            }

            currentRenderedObject = obj;

            this.textBlock_Key.Text = obj.Key.ToString().ToUpperInvariant();
            this.textBox_ApplicationName.Text = obj.ApplicationName;
            this.textBox_CDKey.Text = obj.KeyContent;
            this.textBox_Version.Text = obj.Version;
            this.textBox_Comments.Text = obj.Comments;
            this.checkBox_IsVerified.IsChecked = obj.IsVerified;
            this.checkBox_IsOffical.IsChecked = obj.IsOfficial;

            this.textBox_CDKey.IsReadOnly = !isNew;
            this.checkBox_IsOffical.IsEnabled = !obj.IsOfficial;
            this.button_Delete.IsEnabled = !isNew && !obj.IsOfficial;
        }

        private void listView_QueryResult_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = sender as ListView;
            CDKeyObject cdKeyObj = listView.SelectedItem as CDKeyObject;
            if (cdKeyObj != null)
            {
                FillCDKeyObject(cdKeyObj);
            }
        }

        private void button_Query_Click(object sender, RoutedEventArgs e)
        {
            RefreshResultList();
        }

        private void button_Delete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult objResult = MessageBox.Show("Are You sure to delete?", "Delete Confirmation", MessageBoxButton.YesNo);

            if (objResult == MessageBoxResult.Yes)
            {
                try
                {
                    manager.RemoveCDKey(currentRenderedObject);
                    RefreshApplicationNameList();
                    FillCDKeyObject(null);
                }
                catch (Exception ex)
                {
                    currentRenderedObject = null;
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button_Save_Click(object sender, RoutedEventArgs e)
        {
            CDKeyObject objectToSave = currentRenderedObject;

            objectToSave.ApplicationName = textBox_ApplicationName.Text;
            objectToSave.Version = textBox_Version.Text;
            objectToSave.Comments = textBox_Comments.Text;
            objectToSave.KeyContent = textBox_CDKey.Text;
            objectToSave.IsOfficial = checkBox_IsOffical.IsChecked.HasValue ? checkBox_IsOffical.IsChecked.Value : false;
            objectToSave.IsVerified = checkBox_IsVerified.IsChecked.HasValue ? checkBox_IsVerified.IsChecked.Value : false;

            try
            {
                manager.AddOrUpdateCDKey(objectToSave);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            FillCDKeyObject(objectToSave);

            RefreshApplicationNameList();
        }

        private void checkBox_IsOffical_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;
            button_Delete.IsEnabled = checkbox.IsChecked == null || !checkbox.IsChecked.Value;
        }

        private void textBox_CDKey_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            Clipboard.SetData(DataFormats.UnicodeText, textbox.Text);
        }
    }
}
